package com.hdcircles.autoform.presenter;

import com.hdcircles.autoform.base.RxPresenter;
import com.hdcircles.autoform.model.bean.User;
import com.hdcircles.autoform.model.http.HttpResponse;
import com.hdcircles.autoform.model.http.ImageHelper;
import com.hdcircles.autoform.model.http.RetrofitHelper;
import com.hdcircles.autoform.presenter.contract.UserContract;
import com.hdcircles.autoform.util.LogUtil;
import com.hdcircles.autoform.util.RxUtil;

import javax.inject.Inject;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by hdcircles on 2016/11/16.
 */

public class UserPresenter extends RxPresenter<UserContract.View> implements UserContract.Presenter {

    private RetrofitHelper retrofitHelper;

    private ImageHelper imageHelper;

    @Inject
    public UserPresenter(RetrofitHelper retrofitHelper,ImageHelper imageHelper){
        this.retrofitHelper=retrofitHelper;
        this.imageHelper=imageHelper;
    }

    @Override
    public void getUser(String email) {
        Subscription subscription=retrofitHelper.getUser(email)
                .compose(RxUtil.<HttpResponse<User>>rxScheduleHelper())
                .compose(RxUtil.<User>handleResult())
                .subscribe(new Action1<User>() {
                    @Override
                    public void call(User user) {
                        mView.showUser(user);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void editUser(User user) {
        Subscription subscription=retrofitHelper.updateUser(user)
                .compose(RxUtil.<HttpResponse<String>>rxScheduleHelper())
                .compose(RxUtil.<String>handleResult())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        mView.editUserSuccess(s);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void uploadImage(final String path, final User user) {
        Subscription subscription=retrofitHelper.getToken()
                .compose(RxUtil.<HttpResponse<String>>rxScheduleHelper())
                .compose(RxUtil.<String>handleResult())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        imageHelper.uploadPicture(path,s);
                        imageHelper.setUploadListener(new ImageHelper.uploadListener() {
                            @Override
                            public void success(String url) {
                                user.setPhoto(url);
                                mView.notifyChange();
                            }
                        });
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtil.d(throwable.getMessage());
                        mView.showError("网络错误,请重新操作");
                    }
                });
        addSubscribe(subscription);
    }
}
